package modele;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.hibernate.Session;

public class DAOSpectacle implements IDAO{
	
	private static DAOSpectacle instance = new DAOSpectacle();
	private Session session;
	
	private DAOSpectacle() {
		//Test d'hibernate temporaire
		try {
			Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("Loaded Derby Driver");
		
		   session = HibernateUtil.getSessionFactory().getCurrentSession();
	        session.beginTransaction();

	}
	
	public static DAOSpectacle getInstance(){
		return instance;
	}
	
	public List<Spectacle> getSpectacles(){
		
		//return StubBDSpectacle.getListSpectacles();
		List<Spectacle> spectacles = session.createQuery("from Spectacle").list();
		return spectacles;
	}
	
	public ArrayList<Representation> getRepresentations(){
		List<Spectacle> spectacles = session.createQuery("from Spectacle").list();
		return StubBDSpectacle.getListRepresentations();
	}

	

	public List<Representation> getRepresentations(int idSpect) {
		Spectacle spect = getSpectacle(idSpect);
		if( spect != null){
			return spect.getRepresentations();
		}
		return null;
	}
	
	public Spectacle getSpectacle(int idSpect){

		 Spectacle spectacle = (Spectacle)session.get(Spectacle.class, idSpect);

		 
		return spectacle;
	}

	public Representation getRepresentation(int idRep) {
		

		
		Representation rep =  (Representation)session.get(Representation.class, idRep);
		return rep;
	}
	
public ArrayList<Spectacle> rechercherSpectacles(String paramsReq){
		
		ArrayList<Spectacle> listTempSpectacles = new ArrayList<Spectacle>();
		paramsReq = paramsReq.toLowerCase();
		
		for(int i = 0; i < getSpectacles().size(); i++){
			
			String nomSpectacle =  getSpectacles().get(i).getNom().toLowerCase();
			String nomArtiste =  getSpectacles().get(i).getArtiste().getNom().toLowerCase();
			String prenomArtiste =  getSpectacles().get(i).getArtiste().getPrenom().toLowerCase();
			
			if(nomSpectacle.contains(paramsReq) || 
				nomArtiste.contains(paramsReq)||
				prenomArtiste.contains(paramsReq)){
				
				listTempSpectacles.add(getSpectacles().get(i));
			}
		}
		
		return listTempSpectacles;
	} 
}
